package com.tianma315.web.scanrecord.controller;


import java.util.Arrays;

import com.tianma315.core.common.utils.Result;
import com.tianma315.core.scanrecord.domain.ScanRecordDO;
import com.tianma315.core.scanrecord.service.ScanRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;


/**
 * 
 * <pre>
 * 微信用户查询记录(用于计算用户类型等)
 * </pre>
 * <small> 2020-02-25 14:27:29 | Aron</small>
 */
@Controller
@RequestMapping("/scanrecord/scanRecord")
public class ScanRecordController {
	@Autowired
	private ScanRecordService scanRecordService;
	
	@GetMapping()
	@RequiresPermissions("scanrecord:scanRecord:scanRecord")
	String ScanRecord(){
	    return "scanrecord/scanRecord/scanRecord";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("scanrecord:scanRecord:scanRecord")
	public Result<Page<ScanRecordDO>> list(Integer pageNumber, Integer pageSize, ScanRecordDO scanRecordDTO){
		// 查询列表数据
        Page<ScanRecordDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<ScanRecordDO> wrapper = new EntityWrapper<ScanRecordDO>(scanRecordDTO);
        page = scanRecordService.selectPage(page, wrapper);
        int total = scanRecordService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("scanrecord:scanRecord:add")
	String add(){
	    return "scanrecord/scanRecord/add";
	}

	@GetMapping("/edit/{scanRecordId}")
	@RequiresPermissions("scanrecord:scanRecord:edit")
	String edit(@PathVariable("scanRecordId") Long scanRecordId,Model model){
		ScanRecordDO scanRecord = scanRecordService.selectById(scanRecordId);
		model.addAttribute("scanRecord", scanRecord);
	    return "scanrecord/scanRecord/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("scanrecord:scanRecord:add")
	public Result<String> save( ScanRecordDO scanRecord){
		if(scanRecordService.insert(scanRecord)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("scanrecord:scanRecord:edit")
	public Result<String>  update( ScanRecordDO scanRecord){
		scanRecordService.updateById(scanRecord);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("scanrecord:scanRecord:remove")
	public Result<String>  remove( Long scanRecordId){
		if(scanRecordService.deleteById(scanRecordId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("scanrecord:scanRecord:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] scanRecordIds){
		scanRecordService.deleteBatchIds(Arrays.asList(scanRecordIds));
		return Result.ok();
	}
	
}
